package com.dailyblue.java.standard.thread.test;

import com.dailyblue.java.standard.thread.created.ThreadCreatedA;
import com.dailyblue.java.standard.thread.created.ThreadCreatedB;
import com.dailyblue.java.standard.thread.created.ThreadCreatedC;
import com.dailyblue.java.standard.thread.method.*;
import com.dailyblue.java.standard.thread.safe.SafeA;
import com.dailyblue.java.standard.thread.safe.SafeB;
import com.dailyblue.java.standard.thread.timer.DemoA;

import java.util.Date;
import java.util.Timer;
import java.util.concurrent.FutureTask;

public class App {

    public static void a() {
        // 1⃣️ 新建状态---创建了一个线程
        ThreadCreatedA a1 = new ThreadCreatedA();
        ThreadCreatedA a2 = new ThreadCreatedA();
        // 2⃣️ 就绪状态---线程就绪了，可以执行了
        a1.start();
        a2.start();
    }

    public static void b() {
        // 创建了一个任务
        ThreadCreatedB b1 = new ThreadCreatedB();
        ThreadCreatedB b2 = new ThreadCreatedB();
        // 1⃣️ 新建状态---创建了一个线程 这个t1线程执行了b1任务
        Thread t1 = new Thread(b1);
        Thread t2 = new Thread(b2);
        t1.start();
        t2.start();

    }

    public static void c() throws Exception {
        // 产生了一个任务
        ThreadCreatedC c1 = new ThreadCreatedC();
        ThreadCreatedC c2 = new ThreadCreatedC();
        FutureTask<String> ft1 = new FutureTask<>(c1);
        FutureTask<String> ft2 = new FutureTask<>(c2);
        Thread t1 = new Thread(ft1);
        Thread t2 = new Thread(ft2);
        t1.start();
        t2.start();
        System.out.println("=======");
        // 可以返回结果
        String result1 = ft1.get();
        String result2 = ft2.get();
        System.out.println(result1);
        System.out.println(result2);
    }

    public static void d() throws Exception {
        SafeA sa = new SafeA();
        SafeB sb1 = new SafeB(sa);
        SafeB sb2 = new SafeB(sa);
        Thread t1 = new Thread(sb1);
        Thread t2 = new Thread(sb2);
        t1.start();
        t2.start();
        Thread.sleep(100);
        System.out.println(sa.get());
    }

    public static void e() {
        MethodA a = new MethodA();
        ThreadA a1 = new ThreadA(a);
        ThreadA a2 = new ThreadA(a);
        a1.start();
        a2.start();
    }

    public static void f() throws Exception {
        MethodB mb = new MethodB();
        ThreadB t1 = new ThreadB(mb);
        ThreadB t2 = new ThreadB(mb);
        t1.start();
        Thread.sleep(1000);
        t2.start();
        Thread.sleep(4000);
        mb.b2();
    }

    public static void g() {
        MethodC mc = new MethodC();
        ThreadC c1 = new ThreadC(mc);
        ThreadC c2 = new ThreadC(mc);
        c1.start();
        c2.start();
    }

    public static void h() {
        MethodD md = new MethodD();
        ThreadD t1 = new ThreadD(md);
        ThreadD t2 = new ThreadD(md);
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 应该等t1和t2线程执行完毕后在输出
        md.print();
    }

    public static void i() {
        // 产生了一个定时任务
        DemoA da = new DemoA();
        // 定时调度
        Timer timer = new Timer();
        // 2s之后执行任务
        // timer.schedule(da,2000);
        // 2s之后执行任务并且每隔1s执行一次
        // timer.schedule(da,2000,1000);
        // 具体时间时执行任务
        // timer.schedule(da,new Date(122,8,17,14,9,00));
        timer.schedule(da, new Date(122, 8, 17, 14, 9, 40), 1000);
    }

    public static void j() {
        MethodE e = new MethodE();
        MethodF f = new MethodF();
        ThreadE te = new ThreadE(e, f);
        ThreadF tf = new ThreadF(f, e);
        te.start();
        tf.start();
    }

    public static void main(String[] args) throws Exception {
        j();
    }
}
